package com.zhu.storesmanager.views;

import java.util.ArrayList;
import java.util.List;

import com.zhu.storesmanager.R;
import com.zhu.storesmanager.models.BrandsDataItem;
import com.zhu.storesmanager.models.SyncModel.DataVisitContentSales;
import com.zhu.storesmanager.utils.DataUtils;
import com.zhu.storesmanager.utils.ViewsUtil;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 品牌信息界面
 */
public class BrandInfoView extends LinearLayout implements View.OnClickListener{

    private static final String ROW_SPLIT = DataUtils.SPLIT_LINE;
    private static final String BRAND_SPLIT = DataUtils.SPLIT_ROW;

	private LinearLayout mContentLayout = null;
	private List<BrandsDataItem> mDataList = new ArrayList<BrandsDataItem>();
	private PopBrandInfoTextView mPopTextView = null;

    public BrandInfoView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public BrandInfoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public BrandInfoView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    /**
     * (non-Javadoc)
     * @see android.view.View#onFinishInflate()
     */
    public void onFinishInflate(){
    	mContentLayout = (LinearLayout)findViewById(R.id.bandinfo_content);
    	if(DataUtils.mUserData.isAdmin()){ //管理员隐藏操作
    		findViewById(R.id.brand_operate).setVisibility(View.GONE);	
    	}
    }

    /**
     * setPopTextView
     */
    public void setPopTextView(PopBrandInfoTextView popTextView){
    	mPopTextView = popTextView;
    }

    /**
     * 添加品牌数据
     * @param dataItem 品牌数据
     */
    public void addBrandsData(BrandsDataItem dataItem){
    	if(!isAdded(dataItem)){
    		mDataList.add(dataItem);
        	addDataView(dataItem);
    	}
    }

    /**
     * 检测是否已经添加数据
     * @param dataItem 品牌数据
     * @return ture 已经添加过 false反之
     */
    private boolean isAdded(BrandsDataItem dataItem){
    	for(BrandsDataItem item : mDataList){
    		if(item.mBrandName.equals(dataItem.mBrandName)){
    			return true;
    		}
    	}
    	return false;
    }

    /**
     * 添加品牌数据界面
     * @param dataItem 品牌数据
     */
    private void addDataView(BrandsDataItem dataItem){
    	View view = LayoutInflater.from(getContext()).inflate(R.layout.view_bandinfo_item, null);
    	view.setOnClickListener(this);
    	view.setTag(dataItem);
    	ViewsUtil.setTextViewData(view, R.id.brand_name, dataItem.mBrandName);
    	ViewsUtil.setTextViewData(view, R.id.brand_sellcount, dataItem.getSellCountText());
    	ViewsUtil.setTextViewData(view, R.id.brand_sellmoney, dataItem.getSellMoneyText());
    	View delete = view.findViewById(R.id.brand_delete);
    	if(!DataUtils.mUserData.isAdmin()){ //管理员隐藏操作
        	delete.setOnClickListener(this);
        	delete.setTag(dataItem);
    	}else{
    		delete.setVisibility(View.GONE);
    	}
    	mContentLayout.addView(view);
    }

    /**
     * (non-Javadoc)
     * @see android.view.View.OnClickListener#onClick(android.view.View)
     */
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v != null){
			if(v.getId() == R.id.brand_delete){ //删除品牌
				deleteData((BrandsDataItem)v.getTag());
			}else if(v.getId() == R.id.brand_item){ //选中项
				mPopTextView.showPopTextView(R.string.sales_percent, (BrandsDataItem)v.getTag());
			}
		}
	}

	/**
	 * 删除数据项
	 */
	private void deleteData(BrandsDataItem dataItem){
		int index = mDataList.indexOf(dataItem);
		mDataList.remove(index);
		mContentLayout.removeViewAt(index);
	}

	
	/**
	 * 获取保存数据
	 * @param data 保存的数据
	 */
	public String getSavedData(){
	    String data = "";
	    for(BrandsDataItem item :mDataList){
	        data += item.getSavedData(ROW_SPLIT) + BRAND_SPLIT;
	    }
	    return data;
	}

	/**
	 *设置品牌信息
	 *@param brands 信息
	 */
	public void setBrandData(String brands){
        if(brands != null && brands.length() > 0){
            String[] brandArr = brands.split(BRAND_SPLIT); //拆分品牌数据
            for(String brand : brandArr){ //遍历
                BrandsDataItem item = new BrandsDataItem();
                if(item.setBrandData(brand, ROW_SPLIT)){ //设置数据成功
                    addBrandsData(item); //则添加数据
                }
            }
        }
	}

	public static List<DataVisitContentSales> getBrandData(String brands){
		List<DataVisitContentSales> sales = null;
		if(brands != null && brands.length() > 0){
			sales = new ArrayList<DataVisitContentSales>();
            String[] brandArr = brands.split(BRAND_SPLIT); //拆分品牌数据
            for(String brand : brandArr){ //遍历
            	if(brand != null && brand.length() > 0){
            		String[] branchArr = brand.split(ROW_SPLIT);
            		DataVisitContentSales item = new DataVisitContentSales();
            		item.Brands = branchArr[1];
            		item.BrandsSaleNum = Integer.parseInt(branchArr[2]);
            		item.BrandsSaleAmount = Integer.parseInt(branchArr[3]);
            		sales.add(item);
            	}
            }
        }
		return sales;
	}
}
